<template>
    <div class="tabBar flex-a">
        <nut-button shape="square" block :type="integral < goodRec ? 'default' : 'info'" @click="showPop"
            :disabled="integral < goodRec">立即兑换</nut-button>
        <nut-popup position="bottom" closeable round :style="{ height: '40%' }" v-model:visible="showRound">
            <div class="add-list">
                <div class="add-item flex-a flex-b" v-for="(item, index) in addressList" :key="item.addressId">
                    <div class="left">
                        <div class="flex-a"><span class="name">{{ item.userName }}</span><span class="phone">{{
                            item.userPhone
                        }}</span>
                        </div>
                        <div class="address">{{ item.provinceName + item.cityName + item.regionName + item.detailAddress }}
                        </div>
                    </div>
                    <div class="right">
                        <nut-checkbox v-model="item.checkbox" :label="item.addressId"
                            @change="checked => changeBox(checked, index)"></nut-checkbox>
                    </div>
                </div>
            </div>
            <div class="btn">
                <nut-button shape="square" type="info" @click="toChange">去兑换</nut-button>
            </div>
        </nut-popup>
    </div>
</template>

<script lang="ts">
import Taro, { useRouter } from '@tarojs/taro';
import mitt from "./../../../../utils/mitt";
import { reactive, toRefs, onMounted } from 'vue';
import { } from '@nutui/icons-vue-taro';
import { getAddressList, toOrderRec, getRecharge } from '../../../../api/index'
export default {
    components: {

    },
    setup() {
        const data = reactive({
            id: '',
            showRound: false,
            addressList: [],
            goodsId: '',
            goodRec: '',
            integral: -1
        });
        onMounted(() => {
            const router = useRouter();
            if (router.params.id) {
                data.id = router.params.id
            }
            getRecharge().then(res => {
                if (res.resultCode == 200) {
                    data.integral = res.data.integral
                }
            })
            mitt.on('detail', (val) => {
                data.goodsId = val.goodsId
                data.goodRec = val.sellingPrice
            })
            getAddressList().then(response => {
                if (response.resultCode == 200) {
                    let list = response.data
                    list.forEach(item => {
                        item.checkbox = false
                    })
                    data.addressList = list
                }
            })
        });
        const changeBox = (checked, index) => {
            let list = data.addressList
            if (checked == true) {
                list.forEach((item, n) => {
                    index != n ? item.checkbox = false : ''
                })
            }
        }
        const toChange = () => {
            let list = data.addressList
            let n = list.findIndex(item => {
                return item.checkbox == true
            })
            if (n == -1) {
                Taro.showToast({
                    title: '请先选择收货地址！',
                    icon: 'none',
                    duration: 2000
                })
            } else {
                toOrderRec(data.goodsId, data.goodRec, list[n].addressId).then(res => {
                    if (res.resultCode == 200) {
                        Taro.showToast({
                            title: '兑换成功！',
                            icon: 'success',
                            duration: 2000
                        })
                        data.showRound = false
                    }
                })
            }
        }
        const showPop = () => {
            data.showRound = true
        }
        const show = (type) => {
            mitt.emit("show", [type, true]);
        }
        return {
            ...toRefs(data), show, changeBox, toChange, showPop
        }
    }
};
</script>


<style lang="scss" >
.tabBar {
    position: fixed;
    bottom: 0;
    left: 0;
    background: #FAFAFA;
    box-sizing: border-box;
    width: 100%;
    padding: 20rpx 50rpx;

    .add-list {
        margin-top: 60rpx;
        box-sizing: border-box;
        height: 400rpx;
        overflow-y: auto;

        .add-item {
            padding: 20rpx 30rpx;
            box-sizing: border-box;
            background: #fff;

            .left {
                .name {
                    color: #212121;
                    margin-right: 10rpx;
                    font-size: 34rpx;
                }

                .phone {
                    color: #808080;
                    font-size: 28rpx;
                }

                .address {
                    color: #404040;
                    margin-top: 20rpx;
                    font-size: 30rpx;
                }
            }

            .right {
                color: #C0C0C0;
                font-size: 30rpx;
            }
        }
    }

    .btn {
        position: fixed;
        bottom: 0;
        left: 0;
        background: #fff;
        padding: 20rpx 40rpx;
        box-sizing: border-box;
        width: 100%;
        text-align: center;

        .nut-button {
            width: 400rpx;
            margin: 0 auto;
            border-radius: 10rpx;
        }
    }
}
</style>